<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<!-- saved from url=(0046)http://www.execpc.com/~geezer/osd/fs/index.htm -->

<HTML>
<!-- Mirrored from bos.asmhackers.net/docs/filesystems/ext2/docs/OSD Filesystems.htm by HTTrack Website Copier/3.x [XR&CO'2013], Tue, 07 Oct 2014 11:25:18 GMT -->
<HEAD><TITLE>OSD: Filesystems</TITLE>

<META content="text/html; charset=windows-1252" http-equiv=Content-Type>

<META content="MSHTML 5.00.2919.6307" name=GENERATOR></HEAD>

<BODY><A href="http://www.execpc.com/~geezer/osd/index.htm">OSD Home</A>

<P><!--

http://www.alumni.caltech.edu/~pje/software.html

Reentrant DOS-Compatible File System, by Philip J. Erdelsky

Public domain. Includes disk cache.

-->

<UL>

  <LI><A href="http://www.execpc.com/~geezer/osd/fs/index.htm#fat">FAT 

  filesystem</A> 

  <UL>

    <LI><A 

    href="http://www.execpc.com/~geezer/osd/fs/index.htm#fat-layout">Layout of a 

    FAT volume</A> 

    <LI><A href="http://www.execpc.com/~geezer/osd/fs/index.htm#fat-boot">FAT 

    bootsector and BPB</A> 

    <LI><A href="http://www.execpc.com/~geezer/osd/fs/index.htm#fat-dirent">FAT 

    directory entries</A> 

    <LI><A href="http://www.execpc.com/~geezer/osd/fs/index.htm#fat-fat">The 

    FAT</A> </LI></UL>

  <LI><A href="http://www.execpc.com/~geezer/osd/fs/index.htm#ext2">ext2 

  filesystem</A> 

  <LI><A href="http://www.execpc.com/~geezer/osd/fs/index.htm#iso">ISO-9660 

  (CD-ROM) filesystem</A> 

  <LI><A href="http://www.execpc.com/~geezer/osd/fs/index.htm#overview">Overview 

  of common filesystems</A> 

  <LI><A 

  href="http://www.execpc.com/~geezer/osd/fs/index.htm#snippets">Filesystem code 

  snippets</A> 

  <LI><A href="http://www.execpc.com/~geezer/osd/fs/index.htm#links">Links</A> 

  <LI><A href="http://www.execpc.com/~geezer/osd/index.htm#contact"><B>REPORT 

  BUGS OR ERRORS IN THIS DOCUMENT</B></A> </LI></UL><A name=fat>

<H2>FAT filesystem</H2></A>FAT stands for <I>File Allocation Table</I>, the main 

feature of this filesystem. The FAT filesystem is associated with DOS and some 

versions of Windows. <B><I>VFAT</I></B> (virtual FAT) is FAT with <B>long 

filenames</B>. 

<P>Newly-purchased floppy disks advertised as 'formatted' usually contain a 

FAT12 filesystem. <A name=fat-layout>

<H3>Layout of a FAT volume</H3></A><A 

href="../../../../../www.execpc.com/_geezer/osd/fs/fat.html">A FAT volume contains</A>: 

<UL>

  <LI>boot sector (more than one for FAT32) 

  <LI>root directory (not present for FAT32) 

  <LI>one or more copies of the FAT (almost always 2 copies) 

  <LI>the data area, where files and subdirectories are stored. For FAT32, the 

  root directory is stored in the data area as well. </LI></UL><A name=fat-boot>

<H3>FAT bootsector and BPB</H3></A>The FAT boot sector contains: 

<UL>

  <LI>Code to load and run the DOS kernel 

  <LI>The poorly-named <I><B>BIOS Parameter Block</B></I> (BPB), with disk 

  geometry and filesystem info. The BPB corresponds to the <I>superblock</I> of 

  UNIX filesystems. 

  <LI>Magic values: 55h at offset 510, 0AAh at offset 511. </LI></UL>Layout of 

FAT12/FAT16 bootsector with <B>minimal BPB</B>: <PRE>typedef unsigned char u8;

typedef unsigned short u16;	/* Note: multi-byte values are little-endian */

typedef unsigned long u32;

struct fat_bootsector		/* Warning: this struct must be packed */

{

	u8 jump[3];		/* 16-bit JMP to boot code, or 8-bit JMP + NOP */

	u8 oem_id[8];		/* e.g. 'MSWIN4.0' */

	u16 bytes_per_sector;	/* usu. =512 */

	u8 sectors_per_cluster;

	u16 num_boot_sectors;	/* usu. =1 */

	u8 num_fats;		/* usu. =2 */

	u16 num_root_dir_ents;

	u16 total_sectors;	/* 16-bit; 0 if num sectors &gt; 65535 */

	u8 media_ID_byte;       /* usu. =0F0h */

	u16 sectors_per_fat;

	u16 sectors_per_track;

	u16 heads;

	u32 hidden_sectors;	/* =LBA partition start */

	u32 total_sectors_large;/* 32-bit; 0 if num sectors &lt; 65536 */

	u8 boot_code[474];

	u8 magic[2];		/* 55h, 0AAh */

};                              /* 512 bytes total */</PRE>FAT32 uses additional 

fields (xxx - todo) <A name=fat-dirent>

<H3>FAT directory entries</H3></A>FAT directory entries are 32 bytes long: <PRE>typedef unsigned char u8;

typedef unsigned short u16;	/* Note: multi-byte values are little-endian */

typedef unsigned long u32;

struct fat_dirent               /* Warning: this struct must be packed */

{

        u8 name[8];             /* ALL-CAPS, pad right with spaces */

        u8 ext[3];              /* ALL-CAPS, pad right with spaces */

        u8 attrib;              /* attribute byte */

        u8 reserved;            /* =0 */

        u8 ctime_ms;            /* file creation time, 10ms units */

        u16 ctime;              /* file creation time, in DOS format */

        u16 cdate;              /* file creation date, in DOS format */

        u16 adate;              /* DOS date of last file access */

        u16 st_clust_msw;       /* high 16 bits of starting cluster (FAT32) */

        u16 mtime;              /* DOS time of last file modification */

        u16 mdate;              /* DOS date of last file modification */

        u16 st_clust;           /* starting cluster */

        u32 file_size;          /* in bytes */

};                              /* 32 bytes total */</PRE>DOS times and dates 

are stored in these formats: <PRE>struct dos_time                 /* Warning: this struct must be packed */

{

        unsigned two_secs : 5;  /* low 5 bits: 2-second increments */

        unsigned minutes : 6;   /* middle 6 bits: minutes */

        unsigned hours : 5;     /* high 5 bits: hours (0-23) */

};                              /* 2 bytes total */



struct dos_date                 /* Warning: this struct must be packed */

{

        unsigned date : 5;      /* low 5 bits: date (1-31) */

        unsigned month : 4;     /* middle 4 bits: month (1-12) */

        unsigned year : 7;      /* high 7 bits: year - 1980 */

};                              /* 2 bytes total */</PRE>The Attribute byte is 

similar to the file 'mode' under UNIX filesystems: <PRE>struct attrib                   /* Warning: this struct must be packed */

{

        int read_only : 1;      /* b0 */

        int hidden : 1;

        int system : 1;

        int volume_label : 1;

        int directory : 1;

        int archive : 1;

        int reserved : 2;       /* b6, b7 */

};                              /* 1 byte total */</PRE>FAT directory entries 

contain all <I>metadata</I> for a particular file. This is different from UNIX 

filesystems, which store metadata separately from the directory entries (in 

<I>inodes</I>). FAT directory entries are also used for disk volume labels and 

VFAT long filenames (xxx - todo). <A name=fat-fat>

<H3>The FAT</H3></A>Entries in the FAT can be 12 bits wide (FAT12), 16 bits wide 

(FAT16), or 32 bits wide (FAT32). FAT entries do not necessarily refer to disk 

sectors, but to <B><I>clusters</I></B>, which are <B>groups of contiguous 

sectors</B>. The number of sectors per cluster is always a power of 2. The FAT 

format used is determined solely by the number of clusters in the volume: 

<UL>

  <LI>FAT12: 1...4084 (0FF4h) clusters 

  <LI>FAT16: 4085...65524 (0FFF4h) clusters 

  <LI>FAT32: 65525... clusters </LI></UL>Used FAT entries form <B>singly linked 

lists</I>, indicating which clusters are used by each file or subdirectory. Some 

FAT entry values are special: 

<P>

<TABLE border=1 width="100%">

  <TBODY>

  <TR>

    <TH>FAT special value</TH>

    <TH>FAT12</TH>

    <TH>FAT16</TH>

    <TH>FAT32</TH></TR>

  <TR>

    <TD>Free cluster</TD>

    <TD>0</TD>

    <TD>0</TD>

    <TD>0</TD></TR>

  <TR>

    <TD>Used cluster; pointer to next</TD>

    <TD>2-0FF4h</TD>

    <TD>2-0FFF4h</TD>

    <TD>2-0FFFFFF4h <B>(28-bit)</B></TD></TR>

  <TR>

    <TD>Bad cluster</TD>

    <TD>0FF7h</TD>

    <TD>0FFF7h</TD>

    <TD>0FFFFFF7h <B>(28-bit)</B></TD></TR>

  <TR>

    <TD>Used cluster; last in chain</TD>

    <TD>0FFFh</TD>

    <TD>0FFFFh</TD>

    <TD>0FFFFFFFh <B>(28-bit)</B></TD></TR></TBODY></TABLE><!--

<P>

Example: maximum hard disk partition size for FAT16 with 64 sectors/cluster

 = 65524 clusters * 512-byte sectors * 64 sectors/cluster = 2047 Mbytes.

<P>

Example: <I>DMF</I> floppy disk format, used by Win95 install flopies.

 These floppies have 21 sectors/track instead of 18, so they hold 1.68 Mbytes

 and have 3360 sectors. This is below the 4085-cluster FAT12 limit, however,

 DMF disks have 4 sectors/cluster. This reduces the size of the FATs which,

 along with a smaller root directory, leaves more room for data.

--><A name=ext2>

<H2>ext2 filesystem</H2></A>xxx - to do <A name=iso>

<H2>ISO-9660 (CD-ROM) filesystem</H2></A><A 

href="http://www.execpc.com/~geezer/osd/fs/iso9660.txt">iso9660.txt</A> <A 

name=overview>

<H2>Overview of common filesystems</H2></A><!-- Minix? Rock Ridge? -->Thanks to 

Peter Montagner for information on Macintosh HFS+ filesystem. 

<P>

<TABLE border=1 width="100%">

  <TBODY>

  <TR>

    <TH></TH>

    <TH>FAT12/16 (DOS)</TH>

    <TH>VFAT (Win95), FAT32 (Win98)</TH>

    <TH>NTFS</TH>

    <TH>ext2 (Linux)</TH></TR>

  <TR>

    <TD>Filename length</TD>

    <TD>8.3</TD>

    <TD>255</TD>

    <TD>255</TD>

    <TD>255</TD></TR>

  <TR>

    <TD>Path length</TD>

    <TD>80</TD>

    <TD>260</TD>

    <TD>32767</TD>

    <TD></TD></TR>

  <TR>

    <TD><A href="http://www.execpc.com/~geezer/osd/fs/nametest.c">Illegal 

      characters in filenames</A></TD>

    <TD><TT>(nul) " * + , . / : ; &lt; = &gt; ? [ \ ] |</TT> 

    <TD><TT>(nul) " * / : &lt; &gt; ? \ |</TT> 

    <TD></TD>

    <TD><TT>(nul) /</TT></TD></TR>

  <TR>

    <TD>Legal characters in filenames</TD>

    <TD><TT>(space) ! # $ % &amp; ' ( ) - 0-9 @ A-Z ^ _ ` a-z { } ~</TT> 

      <P>Leading . or space is illegal<BR>Trailing . or space ignored.</P></TD>

    <TD><TT>(space) ! # $ % &amp; ' ( ) + , - . 0-9 ; = @ A-Z [ ] ^ _ ` a-z { 

      } ~</TT> 

      <P>Leading space ignored<BR>Single . or space is illegal<BR>Trailing . or 

      space ignored.</P></TD>

    <TD></TD>

    <TD>everything but<BR><TT>(nul) /</TT></TD></TR>

  <TR>

    <TD>Limit to number of root directory entries</TD>

    <TD>YES</TD>

    <TD>VFAT12/16: YES<BR>FAT32: no</TD>

    <TD>no</TD>

    <TD>no ?</TD></TR>

  <TR>

    <TD>Maximum subdirectory depth</TD>

    <TD>no</TD>

    <TD>no</TD>

    <TD>no ?</TD>

    <TD>no</TD></TR>

  <TR>

    <TD>Directories sorted by name</TD>

    <TD>NO</TD>

    <TD>NO</TD>

    <TD>?</TD>

    <TD>yes?</TD></TR>

  <TR>

    <TD>Block size</TD>

    <TD>FAT12: 512 bytes-4K<BR>FAT16: 2K-32K</TD>

    <TD>4K-32K</TD>

    <TD>512 bytes-64K</TD>

    <TD>1K-</TD></TR>

  <TR>

    <TD>Character set</TD>

    <TD></TD>

    <TD>UCS-2 Unicode</TD>

    <TD>UCS-2 Unicode</TD>

    <TD>Undefined; can be ASCII, Latin-1, UTF-8 Unicode, etc.</TD></TR>

  <TR>

    <TD>Endian</TD>

    <TD>little</TD>

    <TD>little</TD>

    <TD>?</TD>

    <TD>little</TD></TR></TBODY></TABLE>

<P>

<TABLE border=1 width="100%">

  <TBODY>

  <TR>

    <TH></TH>

    <TH>ISO9660 (CD-ROM)</TH>

    <TH>Joliet</TH>

    <TH>Mac HFS+</TH></TR>

  <TR>

    <TD>Filename length</TD>

    <TD>Level 1: 8.3 files, 8 for subdirectories<BR>Level 2/3: 31</TD>

    <TD>64</TD>

    <TD>255</TD></TR>

  <TR>

    <TD>Path length</TD>

    <TD></TD>

    <TD></TD>

    <TD></TD></TR>

  <TR>

    <TD>Illegal characters in filenames</TD>

    <TD><TT>(nul) (space) ! " # $ % &amp; ' ( ) * + , - . / : ; &lt; = &gt; ? 

      @ [ \ ] ^ ` { | } ~</TT> 

      <P>Extension with directory name is not allowed</P></TD>

    <TD></TD>

    <TD><TT>(nul) :</TT></TD></TR>

  <TR>

    <TD>Legal characters in filenames</TD>

    <TD><TT>0-9 A-Z _</TT></TD>

    <TD></TD>

    <TD>everything but<BR><TT>(nul) :</TT></TD></TR>

  <TR>

    <TD>Limit to number of root directory entries</TD>

    <TD>no ?</TD>

    <TD>no</TD>

    <TD>no</TD>

    <TD></TD></TR>

  <TR>

    <TD>Maximum subdirectory depth</TD>

    <TD>8</TD>

    <TD>no</TD>

    <TD></TD></TR>

  <TR>

    <TD>Directories sorted by name</TD>

    <TD>yes</TD>

    <TD>yes</TD>

    <TD></TD></TR>

  <TR>

    <TD>Block size</TD>

    <TD>2K</TD>

    <TD>2K ?</TD>

    <TD></TD></TR>

  <TR>

    <TD>Character set</TD>

    <TD>ASCII</TD>

    <TD>UCS-2 Unicode</TD>

    <TD>UCS-2 Unicode</TD></TR>

  <TR>

    <TD>Endian</TD>

    <TD>Big AND little (committee invention :)</TD>

    <TD></TD>

    <TD>big</TD></TR></TBODY></TABLE><A name=snippets>

<H2>Filesystem code snippets</H2></A><A 

href="http://www.execpc.com/~geezer/osd/fs/fat_info.c">Check disk for FAT 

filesystem, display info</A> 

<P><A href="http://www.execpc.com/~geezer/osd/fs/e2_info.c">Check disk for ext2 

filesystem, display info</A> <A name=links>

<H2>Links</H2></A>Information on FAT12/FAT16 (DOS) filesystems: 

<UL>

  <LI><A 

  href="http://www.nondot.org/sabre/os/articles/FileSystems/">http://www.nondot.org/sabre/os/articles/FileSystems/</A> 



  <LI><A 

  href="http://www.thesky.demon.co.uk/filesystems/fat-filesystem.html">http://www.thesky.demon.co.uk/filesystems/fat-filesystem.html</A>, 

  <A 

  href="http://www.thesky.demon.co.uk/filesystems/fat16-filesystem.html">http://www.thesky.demon.co.uk/filesystems/fat16-filesystem.html</A> 



  <LI><A 

  href="http://home.no.net/tkos/info/fat.html">http://home.no.net/tkos/info/fat.html</A> 



  <LI>Official MS docs for FAT32: <A 

  href="http://www.microsoft.com/hwdev/hardware/fatgendown.asp">http://www.microsoft.com/hwdev/hardware/fatgendown.asp</A>

  </LI></UL>Information on ext2fs (Linux filesystem): 

<UL>

  <LI><A 

  href="http://uranus.it.swin.edu.au/~jn/explore2fs/es2fs.htm">http://uranus.it.swin.edu.au/~jn/explore2fs/es2fs.htm</A> 

  </LI></UL><A name=todo>

<H2>TO DO</H2></A><PRE>- linked list for used and free sectors (FAT family)

- tree for used sectors, bitmap for free (UNIX family)

- subdirectories

- advanced: journalling, striping

- sorted directories let you perform a fast binary search

</PRE><A name=errors>

<H2><A href="http://www.execpc.com/~geezer/osd/index.htm#contact"><B>REPORT BUGS 

OR ERRORS IN THIS DOCUMENT</B></A></H2></A></B></BODY>
<!-- Mirrored from bos.asmhackers.net/docs/filesystems/ext2/docs/OSD Filesystems.htm by HTTrack Website Copier/3.x [XR&CO'2013], Tue, 07 Oct 2014 11:25:19 GMT -->
</HTML>

